<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class provider extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->is_logged_in();
        $this->load->model('provider/provider_model');
    }

    public function index() {
    	$this->config->load('site', TRUE);
        $clients = $this->provider_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['providers'] = $clients;
        $this->_data['status'] = $this->config->item('status', 'site');
        
        $this->load->admintemplate('list', $this->_data);
    }

    public function search() {
    	$this->config->load('site', TRUE);
    	$this->config->load('form_validate', TRUE);
        $params['provider_name'] = $this->input->post('provider_name');
        $clients = $this->provider_model->getSearch($params, $sort = 'status', $order = 'asc');
        $this->_data['providers'] = $clients;
        $this->_data['status'] = $this->config->item('status', 'site');
        
        $this->load->admintemplate('list', $this->_data);
    }

    public function add() {
        $this->load->library('form_validation');
        $this->config->load('form_validate', TRUE);
        $this->config->load('site', TRUE);
        $this->form_validation->set_rules($this->config->item('provider', 'form_validate'));
        $this->_data['status'] = $this->config->item('status', 'site');
        if ($this->form_validation->run() == FALSE) {
            $this->load->admintemplate('add', $this->_data);
        } else {
        	$params['provider_name'] = $this->input->post('provider_name', TRUE);
            $params['address'] = $this->input->post('address', TRUE);
            $params['address_house_number'] = $this->input->post('address_house_number', TRUE);
            $params['address_postcode'] = $this->input->post('address_postcode', TRUE);
            $params['address_place'] = $this->input->post('address_place', TRUE);
            $params['po_box'] = $this->input->post('po_box', TRUE);
            $params['po_box_postcode'] = $this->input->post('po_box_postcode', TRUE);
            $params['po_box_place'] = $this->input->post('po_box_place', TRUE);
            $params['country'] = $this->input->post('country', TRUE);
            $params['termination_email'] = $this->input->post('termination_email', TRUE);
            $client_id = $this->provider_model->insert($params);
            redirect('provider/index');
        }
    }

    public function edit($id) {
        $this->_data['provider'] = $this->provider_model->find_id($id);
        $this->load->library('form_validation');
        $this->config->load('form_validate', TRUE);
        $this->config->load('site', TRUE);
        $this->form_validation->set_rules($this->config->item('provider', 'form_validate'));
        $this->_data['status'] = $this->config->item('status', 'site');
        if ($this->form_validation->run() == FALSE) {
            $this->config->load('site', TRUE);
            $this->load->admintemplate('edit', $this->_data);
        } else {
        	$params['provider_name'] = $this->input->post('provider_name', TRUE);
        	$params['address'] = $this->input->post('address', TRUE);
        	$params['address_house_number'] = $this->input->post('address_house_number', TRUE);
        	$params['address_postcode'] = $this->input->post('address_postcode', TRUE);
        	$params['address_place'] = $this->input->post('address_place', TRUE);
        	$params['po_box'] = $this->input->post('po_box', TRUE);
        	$params['po_box_postcode'] = $this->input->post('po_box_postcode', TRUE);
        	$params['po_box_place'] = $this->input->post('po_box_place', TRUE);
        	$params['country'] = $this->input->post('country', TRUE);
        	$params['termination_email'] = $this->input->post('termination_email', TRUE);
            $this->provider_model->update($id, $params);

            redirect('provider/index');
        }
    }

    public function delete() {
        $id = $this->input->post('id');
        $this->db->trans_start();
        // Delete client
        $delete = (boolean) $this->provider_model->delete($id);
        if ($delete == false) {
            echo 0;
            $this->db->trans_complete();
            die;
        }
        // Delete client's config
        $this->client_config_model->delete($id);
        $this->db->trans_complete();
        if ($this->db->trans_status() !== false) {
            echo 1;
        } else {
            echo 0;
            $this->system_log->log('SQL Failed: ' . $this->db->last_query() . ' - ' . $this->db->_error_message());
        }
        die;
    }

}
